
package com.hyst.seviceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hyst.domain.*;
import com.hyst.repository.PurchaseCourseViewMapper;
import com.hyst.service.CurriculumScheduleService;
import com.hyst.utils.DateUtils;
import com.hyst.utils.GsonUtils;
import com.hyst.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyst.repository.CurriculumScheduleMapper;
import com.hyst.repository.LeaveRequestMapper;
import com.hyst.service.LeaveRequestService;
import com.hyst.utils.UuidTool;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description:
 * @Date：2018年10月07日 
 * @author： bin 
 * @version ：1.0 1.[2018年10月07日]
 */
@Service
public class LeaveRequestServiceImpl implements LeaveRequestService {

	@Autowired
	private LeaveRequestMapper mapper;
	
	@Autowired
	private CurriculumScheduleMapper currMapper;

	@Autowired
	private CurriculumScheduleService curriculumScheduleService;

	@Autowired
	private PurchaseCourseViewMapper purchaseCourseViewMapper;
//	@Override
//	public Page<LeaveRequest> list(LeaveRequest leaveRequest) throws Throwable {
//		final Page<LeaveRequest> page = PageHelper.startPage(leaveRequest.getPageNum(), leaveRequest.getPageSize());
//		mapper.getLeaveRequestList(leaveRequest);
//		return page;
//	}

	@Override
	public Page<LeaveRequest> list(final LeaveRequest leaveRequest) {
		final Page<LeaveRequest> page = PageHelper.startPage(leaveRequest.getPageNum(), leaveRequest.getPageSize());
		try {
            mapper.getLeaveRequestList2(leaveRequest);
            for(LeaveRequest leaveRequest1:page.getResult()){
                try {
                    leaveRequest1.setSubmissionTime(leaveRequest1.getSubmissionTime().split("\\.")[0]);

                    LeaveRequest leaveRequest2 = new LeaveRequest();
                    leaveRequest2.setTeacherId(leaveRequest1.getTeacherId());
                    leaveRequest2.setSubmissionTime(leaveRequest1.getSubmissionTime());

                    List<LeaveRequest> result = mapper.getLeaveRequestDate(leaveRequest2);
                    StringBuilder leaveRequestDateBuilder = new StringBuilder();
                    for (LeaveRequest r : result) {
                        leaveRequestDateBuilder.append(DateUtils.formatDate(r.getClassDate(), "yyyy-MM-dd")).append(",");
                    }
                    if(org.apache.commons.lang3.StringUtils.isNotBlank(leaveRequestDateBuilder.toString())){
                        leaveRequest1.setClassDateStr(leaveRequestDateBuilder.toString().substring(0, leaveRequestDateBuilder.toString().length() - 1));
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        } catch (Exception e){
		    e.printStackTrace();
        }

//		mapper.select(leaveRequest);
		return page;
	}
	
	public LeaveRequest getLeaveInfoById(String uniqueId) throws Exception{
		return mapper.getLeaveInfoById(uniqueId);
	}
	
	/**
	 * 根据时间查询
	 * @param leaveRequest
	 * @return
	 */
//	@Override
//	public Page<LeaveRequest> listByDate(final LeaveRequest leaveRequest) {
//		final Page<LeaveRequest> page = PageHelper.startPage(leaveRequest.getPageNum(), leaveRequest.getPageSize());
//		mapper.listByDate(leaveRequest);
//		return page;
//	}
	
	/**
	 * 根据状态查询
	 * @param leaveRequest
	 * @return
	 */
//	@Override
//	public Page<LeaveRequest> listByStatus(final LeaveRequest leaveRequest) {
//		final Page<LeaveRequest> page = PageHelper.startPage(leaveRequest.getPageNum(), leaveRequest.getPageSize());
//		mapper.listByStatus(leaveRequest);
//		return page;
//	}

	/**
	 * 根据负责人查询
	 * @param leaveRequest
	 * @return
	 */
//	public Page<LeaveRequest> listByAdmin(LeaveRequest leaveRequest){
//		final Page<LeaveRequest> page = PageHelper.startPage(leaveRequest.getPageNum(), leaveRequest.getPageSize());
//		mapper.listByAdmin(leaveRequest);
//		return page;
//	}
	
	/**
	 * 为请假信息分配负责人
	 * @param leaveRequest
	 * @return
	 * @throws Throwable 
	 */
	public int updateAdminForLeave(LeaveRequest leaveRequest) throws Throwable{
		return mapper.updateAdminForLeave(leaveRequest);
	}
	
	/**
	 * 审核请假状态
	 * @param leaveRequest
	 * @return
	 * @throws Throwable 
	 */
	public int updateLeaveRequestStatus(LeaveRequest leaveRequest) throws Throwable{
		return mapper.updateLeaveRequestStatus(leaveRequest);
	}
	
	/**
	 * 查看请假日期的课程安排
	 * @param teacherId
	 * @param leaveDays
	 * @return
	 */
	public Page<CurriculumScheduleView> getCurriculumsByDays(String teacherId,List<String> leaveDays){
		return (Page<CurriculumScheduleView>) currMapper.getCurriculumsByDays(teacherId, leaveDays);
	}


	@Override
	public Page<LeaveRequest> list2(final LeaveRequest leaveRequest) {
		try {
			final Page<LeaveRequest> page = PageHelper.startPage(leaveRequest.getPageNum(), leaveRequest.getPageSize());
			mapper.getLeaveRequestList3(leaveRequest);
			for(LeaveRequest leaveRequest1:page.getResult()){
				try {
					leaveRequest1.setClassDateStr(DateUtils.formatDate(leaveRequest1.getClassDate(), "yyyy-MM-dd"));
//				Student byStudentId = studentMapper.getByStudentId(leaveRequest1.getStudentId());
//				if (byStudentId != null && StringUtils.isNotBlank(byStudentId.getEnName())) {
//					leaveRequest1.setStudentName(byStudentId.getEnName());
//				}
				}catch (Exception e){
					e.printStackTrace();
				}
			}
			return page;
		} catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

	@Transactional
    @Override
    public void updateStatus(Map sqlMap){
		try {
			//修改请假状态
			int count = mapper.updateStatus(sqlMap);
			//修改考勤为老师请假并延长课表
			if ((Integer) sqlMap.get("state") == 1) {
				LeaveRequest request = new LeaveRequest();
				request.setTeacherId(sqlMap.get("teacherId").toString());
				request.setSubmissionTime(sqlMap.get("submissionTime").toString());
				List<LeaveRequest> leaveRequestDate = mapper.getLeaveRequestDate(request);
				for (LeaveRequest leaveRequest : leaveRequestDate) {
					currMapper.updateState(leaveRequest.getCsid());
					CurriculumSchedule curriculumSchedule = new CurriculumSchedule();
					curriculumSchedule.setId(leaveRequest.getCsid());
					CurriculumSchedule curriculumScheduledb = currMapper.selectByPrimaryKey(curriculumSchedule);

					List<Integer> ids = curriculumScheduleService.addLevalClass(curriculumScheduledb.getPurchaseCourseId(), curriculumScheduledb.getClassLength(), null);

					ScheduleLog scheduleLog = ScheduleLog.builder().addClassId(ids).build();
					Map map = new HashMap();
					map.put("id", curriculumScheduledb.getId());
					map.put("log", GsonUtils.toJson(scheduleLog));
					purchaseCourseViewMapper.updateLog(map);
				}
			}
			if (count > 0) {
				//发短信
			}
		}catch (Exception e){
			e.printStackTrace();
		}
    }


	@Override
	public Page<StudentLeaveRequest> studentLeave(final LeaveRequest leaveRequest) {
		try {
			final Page<StudentLeaveRequest> page = PageHelper.startPage(leaveRequest.getPageNum(), leaveRequest.getPageSize());
			mapper.getStudentLeaveList(leaveRequest);
//			for(Map leaveRequest1:page.getResult()){
//				try {
//					leaveRequest1.get("")
//					leaveRequest1.setClassDateStr(DateUtils.formatDate(leaveRequest1.getClassDate(), "yyyy-MM-dd"));
//				}catch (Exception e){
//					e.printStackTrace();
//				}
//			}
			return page;
		} catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}
}
